﻿using FluentAssertions;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using NewRetail.Application.Components.Platform;
using NewRetail.Application.Components.Platform.Entity;
using NewRetail.Application.Core;
using System;
using System.Linq;
using System.Threading.Tasks;
using System.Linq.Dynamic.Core;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Domain.Repositories;

namespace NewRetail.Api.Controllers
{
    /// <summary>
    /// 会员促销规则，生日双倍积分，88折等
    /// </summary>
    [NewRetailRoute("pos-member-promotion-rule")]
    public class PosMemberPromotionRuleController : ReceiptsBaseController<PosMemberPromotionRule>
    {
        private readonly IPosMemberPromotionRuleService _posMemberPromotionRuleService;

        public PosMemberPromotionRuleController(IPosMemberPromotionRuleService posMemberPromotionRuleService)
        {
            this._posMemberPromotionRuleService = posMemberPromotionRuleService;
        }
        public override async Task<ResponseEntity<PagedResultDto<PosMemberPromotionRule>>> QueryPaging([FromBody] PagingRequestDto dto)
        {
            var data = await base.QueryPaging(dto);
            if (data.Data?.Items?.Any() ?? false)
            {
                data.Data.Items = await this._posMemberPromotionRuleService.AttachWarehouse(data.Data.Items.ToArray());
            }
            return data;
        }

        [HttpPost("save-multiple")]
        public async Task<ResponseEntity<PosMemberPromotionRule[]>> SaveMultiple([FromBody] ReceiptNoItemsDto<PosMemberPromotionRule> data)
        {
            var res = await this.Repo.SaveReceiptItems(this.HttpContext.RequestServices, data, "PTR");
            return new ResponseEntity<PosMemberPromotionRule[]>().SetData(res.Items);
        }

        [HttpPost("query-by-receipt-no")]
        public async Task<ResponseEntity<PosMemberPromotionRule[]>> QueryByReceiptNo([FromBody] ReceiptNoDto dto)
        {
            dto.Should().NotBeNull();
            dto.receiptNo.Should().NotBeNullOrEmpty();
            var query = this.SingleStoreService.ApplyToQueryable(this.Repo);
            var data = await query.Where(x => x.receiptNo == dto.receiptNo).ToArrayAsync();

            return new ResponseEntity<PosMemberPromotionRule[]>().SetData(data);
        }
    }
}
